﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models;
using tracker.Models.DAL;
using tracker.Models.DTO;

namespace tracker.Controllers
{
    public class HandlerDeviceListController : Controller
    {
        //
        // GET: /HandlerDeviceList/
        [CallAjaxFilter]
        public JsonStringResult Index()
        {
            string result = "";
            string currentUserGroupID = Session[Common.USER_GROUPID].ToString();
            List<EUserLogin> lstUser = new List<EUserLogin>();
            UserLoginDAL userDAL = new UserLoginDAL();
            List<DeviceInfo> listDeviceInfo = new List<DeviceInfo>();
            if (currentUserGroupID == "3")
            {
                //Get all
                lstUser = userDAL.getUserLoginByUserAdministrator();
            }
            else if (currentUserGroupID == "4")
            {
                //Get all group
                lstUser = userDAL.getUserLoginByParentCode(Session[Common.USER_SECUTIRY_CODE].ToString());
            }
            else 
            {
                //Get device only
                lstUser = userDAL.getUserLoginBySecurityCode(Session[Common.USER_SECUTIRY_CODE].ToString());
            }
            if (lstUser != null)
            {
                for (int i = 0; i < lstUser.Count; i++)
                {
                    List<EDevice> lstDevice = new DeviceDAL().getByUserName(lstUser[i].Username);
                    if (lstDevice == null)
                    {
                        continue;
                    }

                    for (int j = 0; j < lstDevice.Count; j++)
                    {
                        DeviceInfo entDeviceInfo = new DeviceInfo();
                        TrackLogDAL tlDAL = new TrackLogDAL();
                        ETrackLog entTrackLog = tlDAL.getLastByDeviceKey(lstDevice[j].DeviceKey);
                        ETrackLog entTrackLogAlarm = tlDAL.getLastAlarmByDeviceKey(lstDevice[j].DeviceKey);

                        entDeviceInfo.devicetype = lstDevice[j].DeviceTypeID.ToString();
                        if (entTrackLog != null)
                        {
                            entDeviceInfo.journey = entTrackLog.Journey;
                            //entDeviceInfo.lastupdate = entTrackLog.RecieveTime.convertGMTClientTime().ToString("yyyy-MM-dd hh:mm:ss tt");
                            entDeviceInfo.lastupdate = entTrackLog.RecieveTime.ToString("yyyy-MM-dd hh:mm:ss tt");
                            entDeviceInfo.latitude = entTrackLog.Latitude;
                            entDeviceInfo.longitude = entTrackLog.Longitude;
                        }

                        entDeviceInfo.actionretry = "";
                        entDeviceInfo.atype = false;
                        entDeviceInfo.choosed = false;
                        //This line is confuse
                        //entDeviceInfo.distance = entDeviceInfo.distance;
                        entDeviceInfo.basemileage = 0;
                        entDeviceInfo.online = false;
                        entDeviceInfo.process = "";
                        entDeviceInfo.qtipretry = "";
                        entDeviceInfo.retry = "";
                        entDeviceInfo.routedata = "";
                        entDeviceInfo.routename = "";
                        entDeviceInfo.servicedue = 0;

                        if (entTrackLogAlarm != null)
                        {
                            entDeviceInfo.qtipalarm = "";                            
                            entDeviceInfo.actionalarm = "icon-none";
                            //entDeviceInfo.alarmtime = entTrackLogAlarm.RecieveTime.convertGMTClientTime().ToString("yyyy-MM-dd hh:mm:ss tt");
                            entDeviceInfo.alarmtime = entTrackLogAlarm.RecieveTime.ToString("yyyy-MM-dd hh:mm:ss tt");
                            entDeviceInfo.alarminfo = entTrackLogAlarm.AlarmCode;
                            entDeviceInfo.alarmlatitude = entTrackLogAlarm.Latitude;
                            entDeviceInfo.alarmlongitude = entTrackLogAlarm.Longitude;
                            entDeviceInfo.alarmspeed = entTrackLogAlarm.Speed;
                        }
                        else
                        {
                            entDeviceInfo.qtipalarm = "Không báo động |" + entDeviceInfo.lastupdate;
                        }
                        entDeviceInfo.sssid = lstDevice[j].DeviceKeySecurity;
                        entDeviceInfo.trackername = lstDevice[j].DeviceName;
                        entDeviceInfo.iconname = lstDevice[j].PictureOfDevide;

                        entDeviceInfo.username = lstUser[i].Username;
                        
                        listDeviceInfo.Add(entDeviceInfo);
                         
                             
                    }
                }
            }
            result = Utilities.getJsonString(listDeviceInfo, string.Empty, 2);
            return new JsonStringResult(result);
        }

    }
}
